/**
 *
 */
package com.cw.jeeyt.service.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.utils.SpringContextHolder;
import com.cw.jeeyt.service.bo.system.SystemDictCacheBo;
import com.cw.jeeyt.service.intf.system.SystemDictService;
import com.cw.lang.common.utils.CollectionUtil;

/**
 * Description:字典项获取工具类
 *
 * @author chenw
 * @date 2016年6月12日 上午9:47:51
 */
public class DataDicUtils {
    @Autowired
    private static SystemDictService systemDictService = SpringContextHolder.getBean(SystemDictService.class);
    private static Logger logger = LoggerFactory.getLogger(DataDicUtils.class);

    /**
     * 加载form页面数据字典项
     *
     * @param jsonArray
     * @return
     */
    public static List<Map<String, Object>> getDataDicItem(JSONArray jsonArray) {

        List<Map<String, Object>> dictList = null;
        SystemDictCacheBo systemDictBo = null;
        if (null == systemDictService) {
            logger.error("获取数据字典服务Service为空,请联系技术人员！");
            return dictList;
        }
        dictList = new ArrayList<>();
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject obj = (JSONObject) jsonArray.get(i);
            String dicId = (String) obj.get("id");
            String name = (String) obj.get("name");
            String dict = (String) obj.get("dict");
            String divId = (String) obj.get("divId");
            String divName = (String) obj.get("divName");
            String dicType = (String) obj.get("dicType");
            String length = (String) obj.get("length");
            String value = (String) obj.get("value");
            String method = (String) obj.get("method");
            String disabled = (String) obj.get("disabled");
            String readonly = (String) obj.get("readonly");
            String required = (String) obj.get("required");
            String defaultValue = (String) obj.get("defaultValue");
            String chooseList = (String) obj.get("chooseList");// 选择要加载的字典项
            String subList = (String) obj.get("subList");// 移除要屏蔽的字典项
            try {
                Map<String, Object> map = new HashMap<>();
                systemDictBo = systemDictService.getSystemDictByItemCode(dict);
                if (null == systemDictBo) {
                    continue;
                }
                map.put("dict", dict);
                map.put("code", systemDictBo.getItemCode());
                map.put("id", dicId);
                map.put("name", name);
                map.put("divId", divId);
                map.put("divName", divName);
                map.put("dicType", dicType);
                map.put("length", length);
                map.put("value", value);
                map.put("method", method);
                map.put("disabled", disabled);
                map.put("readonly", readonly);
                map.put("required", required);
                map.put("defaultValue", defaultValue);
                List<SystemDictCacheBo> systemDictList = systemDictService.getItemDictListByParentId(systemDictBo.getId());
                if (StringUtils.isNotEmpty(chooseList)) {
                    List<SystemDictCacheBo> choose_systemDictList = new ArrayList<>();
                    for (SystemDictCacheBo m : systemDictList) {
                        if (chooseList.contains(m.getItemValue())) {
                            choose_systemDictList.add(m);
                        }
                    }
                    systemDictList = choose_systemDictList;
                }
                List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
                if (systemDictList != null && !systemDictList.isEmpty()) {
                    for (SystemDictCacheBo m : systemDictList) {
                        if (StringUtils.isNotEmpty(subList)) {
                            if (subList.contains(m.getItemValue())) {
                                continue;
                            }
                        }
                        Map<String, Object> itemMap = new HashMap<>();
                        itemMap.put("itemName", m.getItemName());
                        itemMap.put("itemValue", m.getItemValue());
                        itemList.add(itemMap);
                    }
                }
                map.put("itemList", itemList);
                dictList.add(map);
            } catch (Exception e) {
            }// 暂时不做处理
        }
        return dictList;
    }

    /**
     * 转换页面列表数据字典项
     *
     * @param jsonArray 要转换的对象集合
     * @param dictMap 数据字典列表集合 key 字段名称，value 数据字典项
     * @return
     */
    public static JSONArray convertDict(JSONArray jsonArray, Map<String, Object> dictMap) {
        SystemDictCacheBo systemDictBo;

        if (dictMap == null || dictMap.isEmpty()) {
            return null;
        }
        Set keys = dictMap.keySet();
        Iterator it = keys.iterator();
        List<String> itemCodes = new ArrayList<>();
        List<String> fields = new ArrayList<>();
        while (it.hasNext()) {
            String key = (String) it.next();
            String value = (String) dictMap.get(key);
            itemCodes.add(value);
            fields.add(key);
        }
        Map<String, SystemDictCacheBo> params = systemDictService.getSystemDictByItemCodes(itemCodes);
        if (CollectionUtil.isEmpty(params)) {
            return null;
        }
        JSONArray array = new JSONArray();
        if (jsonArray != null && jsonArray.size() > 0) {
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject obj = (JSONObject) jsonArray.get(i);
                if (obj != null) {
                    for (String field : fields) {
                        // 根据数据字典项查询数据字典信息
                        String value = (String) dictMap.get(field);
                        systemDictBo = params.get(value);
                        if (null == systemDictBo) {
                            continue;
                        }
                        // 数据字典具体子项
                        List<SystemDictCacheBo> itemList = systemDictBo.getChildList();
                        if (itemList != null && itemList.size() > 0) {
                            if (obj.containsKey(field)) {
                                boolean isExist = false;
                                Object object = obj.get(field);
                                String values = "";
                                if (object instanceof String) {
                                    values = (String) object;
                                } else if (object instanceof Integer) {
                                    values = String.valueOf(object);
                                }
                                if (StringUtils.isNotEmpty(values)) {
                                    if (values.indexOf(",") > -1) {
                                        String itemNames = "";
                                        for (SystemDictCacheBo systemDict : itemList) {
                                            String itemName = systemDict.getItemName();
                                            String itemValue = systemDict.getItemValue();
                                            if (obj.get(field)
                                                .toString()
                                                .contains((itemValue))) {
                                                itemNames += itemName + ",";
                                                isExist = true;
                                            }
                                        }
                                        if (StringUtils.isNotEmpty(itemNames)) {
                                            obj.put(field + "Cn", itemNames.substring(0, itemNames.length() - 1));
                                        } else {
                                            obj.put(field + "Cn", "");
                                        }
                                    } else {
                                        for (SystemDictCacheBo systemDict : itemList) {
                                            String itemName = systemDict.getItemName();
                                            String itemValue = systemDict.getItemValue();
                                            if (obj.get(field)
                                                .equals(itemValue)) {
                                                obj.put(field + "Cn", itemName);
                                                isExist = true;
                                            }
                                        }
                                    }
                                }
                                if (!isExist) {
                                    obj.put(field + "Cn", "");
                                }
                            }
                        }
                    }
                }
                array.add(obj);
            }
        }

        return array;
    }
}
